package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.Activity.TestActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Testbill;
import com.tianxin.workbao.workBao.dataProvide.TestProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Testbill> mtests;
    private Context mContext;


    public TestAdapter(List<Testbill> list, Context context) {
        this.mtests = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mtests.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_bill_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mtests.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (mtests.size() > 0) {
            return mtests.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView numberTV;
        TextView testTimeTV;
        TextView carsTV;
        TextView weightTV;
        ImageView arrowIV;
        ImageButton deleteBtn;
        ExpandView expandView;
        TextView waterTV;
        TextView ashValueTV;
        TextView sulfurValueTV;
        TextView heatValueTV;
        TextView samplingNumberTV;
        TextView supplyCompanyTV;
        TextView CoalNameTV;
        TextView samplingUserTV;
        TextView testUserTV;
        TextView remarkTV;


        public ViewHolder(View itemView) {
            super(itemView);
            numberTV = (TextView) itemView.findViewById(R.id.number);
            testTimeTV = (TextView) itemView.findViewById(R.id.test_time);
            carsTV = (TextView) itemView.findViewById(R.id.cars);
            weightTV = (TextView) itemView.findViewById(R.id.weight);
            arrowIV = (ImageView) itemView.findViewById(R.id.arrow);
            deleteBtn = (ImageButton) itemView.findViewById(R.id.delete_btn);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            waterTV = (TextView) itemView.findViewById(R.id.water);
            ashValueTV = (TextView) itemView.findViewById(R.id.ash_value);
            sulfurValueTV = (TextView) itemView.findViewById(R.id.sulfur_value);
            heatValueTV = (TextView) itemView.findViewById(R.id.heat_value);
            samplingNumberTV = (TextView) itemView.findViewById(R.id.sampling_number);
            supplyCompanyTV = (TextView) itemView.findViewById(R.id.supply_company);
            CoalNameTV = (TextView) itemView.findViewById(R.id.coal_name);
            samplingUserTV = (TextView) itemView.findViewById(R.id.sampling_user);
            testUserTV = (TextView) itemView.findViewById(R.id.test_user);
            remarkTV = (TextView) itemView.findViewById(R.id.remark);
        }

        public void BindViewData(Testbill testbill) {
            numberTV.setText(testbill.getTbnum());
            testTimeTV.setText(TimeUtils.parseDateTime(testbill.getTesttime()));
            carsTV.setText(testbill.getWeightscarnum() + " 车");
            weightTV.setText(testbill.getWeights() + " 吨");
            waterTV.setText("水价 " + testbill.getAnalyzewatervalue());
            ashValueTV.setText("灰份 " + testbill.getAshvalue());
            sulfurValueTV.setText("硫 " + testbill.getTotalsulfurvalue());
            Float heatValue = testbill.getHeatvalue();
            if (heatValue == null || heatValue < 1) {
                heatValueTV.setVisibility(View.GONE);
            } else {
                heatValueTV.setText("热量 " + heatValue);
            }
            samplingNumberTV.setText(testbill.getSamplingnum());
            supplyCompanyTV.setText(testbill.getSupplycpname());
            CoalNameTV.setText(testbill.getCoaltype());
            samplingUserTV.setText("采样："+ testbill.getSamplinguname());
            testUserTV.setText("化验："+ testbill.getTestuname());
            String remark = testbill.getRemark();
            if (remark == null || remark.length() < 2) {
                remarkTV.setVisibility(View.GONE);
            } else {
                remarkTV.setText(remark);
            }
        }

        public void setViewListener(final int position) {
            arrowIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (expandView.isExpand()) {
                        expandView.collapse();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                    } else {
                        expandView.expand();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                    }
                }
            });
            deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TestProvide testProvide = new TestProvide(mContext, new UpdateListViewListener() {
                        @Override
                        public void onRefreshData(Object result) {

                        }

                        @Override
                        public void onLoadMoreData(Object result) {

                        }

                        @Override
                        public void showMessage(String msg) {
                            if (msg == "ok") {
                                expandView.setVisibility(View.GONE);
                                mtests.remove(position);
                                notifyDataSetChanged();
                                Toast.makeText(mContext,"操作成功" , Toast.LENGTH_LONG).show();
                            } else if (msg.contains("failure")) {
                                Toast.makeText(mContext, "操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
                            }

                        }

                        @Override
                        public void showProgress() {
                            TestActivity activity = (TestActivity) mContext;
                            activity.getSweetAlertDialog(activity).showDialog("",0);
                        }

                        @Override
                        public void hideProgress() {
                            TestActivity activity = (TestActivity) mContext;
                            activity.getSweetAlertDialog(activity).dismiss();
                        }
                    });
                    testProvide.deleteTest((int) mtests.get(position).getId());
                }
            });
        }
    }
}
